package com.informa.velocity;

import static com.informa.utils.IOUtils.textFrom;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.Map;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.context.Context;
import org.springframework.core.io.Resource;

public class VelocityUtils {

	public static String renderTemplate(String template, Map<String,? extends Object> params) {
		
		Context context = new VelocityContext(params);
		StringWriter writer = new StringWriter();
		
		try {
			Velocity.evaluate( context, writer, "template", new StringReader(template) );
		} catch (Exception e) {
				throw new RuntimeException(e);
		}
		
		return writer.toString();
	}

	public static String renderTemplate(Resource template,
			Map<String, ? extends Object> params) {
		
		return renderTemplate( textFrom( template), params );
	}
	
}
